//
//  ZDDNetworkTools.swift
//  小蜗牛_新浪微博
//
//  Created by 周冬冬 on 16/6/26.
//  Copyright © 2016年 我的mac. All rights reserved.
//

import UIKit

import AFNetworking
//请求方式枚举
enum MethodType:String {

    case GET = "GET"

    case POST = "POST"


}
class ZDDNetworkTools: AFHTTPSessionManager {
    
    //全局访问点
    static let sharedTools:ZDDNetworkTools = {
    
        let tools = ZDDNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    
    }()
    
    typealias SuccessClosure = (response:AnyObject?)->()
    typealias FailureClosure = (error:NSError)->()
    /**
    请求数据方法
    
    - parameter method:     方式
    - parameter urlString:  url
    - parameter parameters: 参数
    - parameter success:    成功
    - parameter failure:    失败
    */
    
    func request(method:MethodType,urlString:String,parameters:AnyObject?,success:SuccessClosure,failure:FailureClosure){
    
    
        //GET请求
        if method == .GET {
        
            self.GET(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                
                success(response: res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
            })
        
        }else{
        //POST请求
            self.POST(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                success(response: res)
            
                }, failure: { (_, err) -> Void in
                failure(error: err)
            })
        
        }
    
    
    
    }
    
}
extension ZDDNetworkTools {


    func oauthLoadUserAccount(code:String,success:SuccessClosure,failure:FailureClosure) {
    //请求数据
        let urlString = "https://api.weibo.com/oauth2/access_token"
       
    let params = [
        "client_id":APPKEY,
        "client_secret":APPSECRET,
        "grant_type":"authorization_code",
        "code":code,
        "redirect_uri":REDIRECT_URI
        
        ]
    self.request(MethodType.POST, urlString: urlString, parameters: params, success: success, failure: failure)
    
    }

//请求个人信息数据
    func oauthLoadUserInfo(userAccont: ZDDUserAccount, success: SuccessClosure, failure: FailureClosure) {
    
        let urlString = "https://api.weibo.com/2/users/show.json"
    
        //请求数据
        let params = [
        "access_token":userAccont.access_token!,
            "uid":userAccont.uid!
        
        
        ]
    
    self.request(MethodType.GET, urlString: urlString, parameters: params, success: success, failure: failure)
    }


    //  请求首页数据
    func homeloadStatusData(sinceId:Int64, maxId:Int64, success: SuccessClosure, failure: FailureClosure){
    
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params = ["access_token":ZDDUserAccountTools.sharedTools.accessToken!,
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"
        
        ]
  //        since_id	false	int64	若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。
  //        max_id	false	int64	若指定此参数，则返回ID小于或等于max_id的微博，默认为0。
    self.request(MethodType.GET, urlString: urlString, parameters: params, success: success, failure: failure)
    }

    //发布微博
    func composeUpdate(status:String,success: SuccessClosure, failure: FailureClosure){
    
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        let params = ["access_token":ZDDUserAccountTools.sharedTools.accessToken!,
            "status":status
        ]
    
    self.request(.POST, urlString: urlString, parameters: params, success: success, failure: failure)
    
    
    }
    //发布带有图片的微博
    func comoposeUpload(status:String,imageList:[UIImage],success: (response: AnyObject?)->(), failure:(error: NSError)->()){
    
        // 请求url
        let URLString = "https://upload.api.weibo.com/2/statuses/upload.json"
        // 请求参数
        let params = [
            "access_token":ZDDUserAccountTools.sharedTools.accessToken!,
            "status":status
            
        ]
    
    POST(URLString, parameters: params, constructingBodyWithBlock: { (formdata) -> Void in
        //遍历数组
        for image in imageList {
        
            let data = UIImagePNGRepresentation(image)!
            
            formdata.appendPartWithFileData(data, name: "pic", fileName: "oooo", mimeType: "application/octet-stream")
        
        
        }
        
        }, progress: nil, success: { (_, response) -> Void in
            
            success(response: response)
        }) { (_, error) -> Void in
            failure(error: error)
        }
    
    
    
    }
}

















